﻿using LD.Host;
using LD.Host.Mes;
using System;
using System.Collections;
using System.Security.Policy;
using System.Text;
using System.Xml;
using 右下角弹窗.DiagShow;

namespace LD.MES
{
    public partial class FrmMesByHYFSK : BaseFrm
    {
        public FrmMesByHYFSK()
        {
            InitializeComponent();
            this.Shown += MesHelperSharpFrm_Shown;
        }

        private void MesHelperSharpFrm_Shown(object sender, EventArgs e)
        {
            base.Permission(UserType.工程师);

            if (this.IsDisposed)
                return;
            Init();
        }
        private void Init()
        {
            if (Cache.GetCache(CONST.HYFSK_DATA_DAY) == null)
                Cache.SetCache(CONST.HYFSK_DATA_DAY, 30);

            if (Cache.GetCache(CONST.SYS_SOURCE_RUN) == null)
                Cache.SetCache(CONST.SYS_SOURCE_RUN, false);

            this.txt_stationId.Text = (string)Cache.GetCache(CONST.HYFSK_MES_STATIONNUM);
            this.txt_port.Text = (string)Cache.GetCache(CONST.HYFSK_MES_INTERFACE);
            this.txt_stationCode.Text = (string)Cache.GetCache(CONST.HYFSK_MES_STATIONCODE);
            this.txt_mesUrl.Text = (string)Cache.GetCache(CONST.HYFSK_MES_URL);
            this.sw_ismes.Checked = (bool)Cache.GetCache(CONST.HYFSK_MES_ENABLED);
            this.txt_upVal.Text = (float)Cache.GetCache(CONST.HYFSK_PRESS_UP) + "";
            this.txt_downVal.Text = (float)Cache.GetCache(CONST.HYFSK_PRESS_DOWN) + "";
            this.txt_pressTime.Text = (int)Cache.GetCache(CONST.HYFSK_PRESS_TIMER) + "";
            this.sw_sourceRun.Checked = (bool)Cache.GetCache(CONST.SYS_SOURCE_RUN);
            this.txt_dataDay.Text = (int)Cache.GetCache(CONST.HYFSK_DATA_DAY) + "";
        }
        public void Save()
        {
            Cache.SetCache(CONST.HYFSK_MES_STATIONNUM, this.txt_stationId.Text);
            Cache.SetCache(CONST.HYFSK_MES_STATIONCODE, this.txt_stationCode.Text);
            Cache.SetCache(CONST.HYFSK_MES_INTERFACE, this.txt_port.Text);
            Cache.SetCache(CONST.HYFSK_MES_URL, this.txt_mesUrl.Text);
            Cache.SetCache(CONST.HYFSK_MES_ENABLED, this.sw_ismes.Checked);
            Cache.SetCache(CONST.SYS_SOURCE_RUN, this.sw_sourceRun.Checked);
            if (float.TryParse(this.txt_upVal.Text, out var upVal))
                Cache.SetCache(CONST.HYFSK_PRESS_UP, upVal);
            if (float.TryParse(this.txt_downVal.Text, out var downVal))
                Cache.SetCache(CONST.HYFSK_PRESS_DOWN, downVal);
            if (int.TryParse(this.txt_pressTime.Text, out var pressTime))
                Cache.SetCache(CONST.HYFSK_PRESS_TIMER, pressTime);
            if (int.TryParse(this.txt_dataDay.Text, out var dataDay))
                Cache.SetCache(CONST.HYFSK_DATA_DAY, dataDay);
            Dialog dialog = new Dialog();
            dialog.Success("保存成功");
        }

        private void btn_upload_Click(object sender, EventArgs e)
        {
            string machine = (string)Cache.GetCache(CONST.DPT_MACHINE);
            Hashtable table = new Hashtable();
            table.Add("station_num", this.txt_stationId.Text);
            table.Add("station_code", this.txt_stationCode.Text);
            table.Add("wip_no", this.txt_barcode.Text);
            bool result = MesByHYFSK.Check(this.txt_mesUrl.Text, this.txt_port.Text, table);
            //if (result)
            //    this.txt_result.Text = "上传成功";
            //else
            //    this.txt_result.Text = "上传失败";
        }
    }
}
